package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.practicegrades.PracticeGradesAddDto;
import com.teacher.pojo.dto.practicegrades.PracticeGradesGetDto;
import com.teacher.pojo.dto.practicegrades.PracticeGradesUpdateDto;
import com.teacher.pojo.entity.PracticeGrades;
import com.teacher.pojo.vo.practicegrades.PracticeGradesGetVo;
import com.teacher.service.IPracticeGradesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 实习成绩 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/pc/practice-grades")
@Api(tags = "实习成绩")
public class PracticeGradesController {

    @Autowired
    private IPracticeGradesService iPracticeGradesService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加实习成绩
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加实习成绩 practiceGrades_add")
    @PostMapping
    @PreAuthorize("hasAuthority('practiceGrades_add')")
    public CommonResult<String> add(@RequestBody @Validated PracticeGradesAddDto dto) {
        PracticeGrades practiceGrades = new PracticeGrades();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, practiceGrades);
        if (iPracticeGradesService.save(practiceGrades)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新实习成绩
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新实习成绩 practiceGrades_update")
    @PutMapping
    @PreAuthorize("hasAuthority('practiceGrades_update')")
    public CommonResult<String> update(@RequestBody @Validated PracticeGradesUpdateDto dto) {
        PracticeGrades practiceGrades = new PracticeGrades();
        BeanUtils.copyProperties(dto, practiceGrades);
        if (iPracticeGradesService.updateById(practiceGrades)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除实习成绩
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除实习成绩 practiceGrades_delete")
    @PreAuthorize("hasAuthority('practiceGrades_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iPracticeGradesService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询实习成绩
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询实习成绩 practiceGrades_get")
    @PreAuthorize("hasAuthority('practiceGrades_get')")
    public CommonResult<IPage<PracticeGradesGetVo>> getPage(PracticeGradesGetDto dto) {
        return iPracticeGradesService.getPage(dto);
    }

    /**
     * 导入实习成绩
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入实习成绩 practiceGrades_upload")
    @PreAuthorize("hasAuthority('practiceGrades_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iPracticeGradesService.upload(file);
    }

}

